﻿package  
{
	import flash.display.*;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	
	/**
	 * Line ジェネレーター　兼　マウスポイントレコーダー
	 */
	public class Line extends Sprite
	{
		
		private var _pts :/*Point*/Array
		
		private var _now :int;
		private var _len :int;
		
		private var _num :int;	// 長さ
		private var _sp  :int;	// スピード
		
		
		private var _lb :Number;	// 線の初期太さ
		private var _ld :Number;	// 線の変化量
		
		private var _col :uint;
		
		public function Line( pts :/*Point*/Array = null, lb :Number = 1, ld :Number = 0, col :uint = 0xffffff ) 
		{
			setPts( pts );
			
			
			_lb = lb;
			_ld = ld;
			_col = col;
			
			// filters = [ new GlowFilter(0xff00ff), new BlurFilter(0, 0, 1) ]
			
			// filters = [ new GlowFilter(), new BlurFilter(3, 3) ]
			// filters = [ new BlurFilter(3,3) ]
		}
		
		/* Point をセット。何もないと、デフォルト値が入る */
		public function setPts ( pts :/*Point*/Array = null ) 
		{
			if ( pts )
			{
				if ( pts[0] is Point )	_pts = pts;
				else					_pts = Utils.numToPts( pts );
			}
			else		_pts = Data.getPoints();
		}
		/* Point を追加。 */ 
		public function addPts ( pts :/*Point*/Array ) 
		{
			if ( pts[0] is Point )	_pts = _pts.concat( pts );
			else					_pts = _pts.concat( Utils.numToPts( pts ) );
		}
		/* 太さ,  太くなるスピード, 色 */ 
		public function setParam ( lb :Number = 1, ld :Number = 0, col :uint = 0xffffff ) :void
		{
			_lb = lb;
			_ld = ld;
			_col = col;
		}
		
		/* 持続長さ, 1フレあたりのスピード */
		public function start ( num :int = 50, sp :int = 5 ) :void
		{
			_now = 0;
			_len = _pts.length;
			
			_num = num;		// 持続長さ
			_sp  = sp;		// スピード調整
			
			removeEventListener( Event.ENTER_FRAME, _enterFrame );
			addEventListener( Event.ENTER_FRAME, _enterFrame );
		}
		/* 停止 */
		public function stop ( ) :void
		{
			removeEventListener( Event.ENTER_FRAME, _enterFrame );
		}
		
		private function _enterFrame ( evt :Event ) :void
		{
			var n :int = _now - _num;
			if ( n < 0 ) n = 0;
			
			
			graphics.clear();
			
			var b :Number = _lb;
			graphics.lineStyle( b, _col );
			
			if ( n < _len )	graphics.moveTo( _pts[n].x, _pts[n].y );
			
			for ( var i :int = n+1; i < _now && i < _len; i ++ )
			{
				graphics.lineStyle( b, _col );
				
				//var cx :Number = (_pts[i - 1].x + _pts[i].x) / 2 + Math.random() * 4 - 2;
				//var cy :Number = (_pts[i - 1].y + _pts[i].y) / 2 + Math.random() * 4 - 2;
				//var cx :Number = Math.random() * 640;
				//var cy :Number = Math.random() * 480;
				
				//graphics.curveTo( cx, cy, _pts[i].x, _pts[i].y );
				graphics.lineTo( _pts[i].x, _pts[i].y );
				
				b += _ld;
			}
			
			if ( _now < _len + _num + 5 )
			{
				_now += _sp;
			}
			else
			{
				_now = 0;
			}
			
		}
		
	}
}